<?php
/**
 * PrivateMessageController
 *
 * @author Nguyen Hai Ha
 * @version 1.0
 */

class Member_MessageController extends Zend_Controller_Action {
	protected $_baseUrl;
	protected $_contextPath;
	protected $_utilities;
	protected $_auth;
	protected $_acl;

	public function init() {
		$this->_baseUrl = $this->_request->getBaseURL ();
		$this->_contextPath = "member/message";

		$this->view->assign ( "baseUrl", $this->_baseUrl );
		$this->view->assign ( "contextPath", $this->_contextPath );

		$this->_utilities = new Utilities ( );
		$this->view->assign ( "utilities", $this->_utilities );

		$this->_auth = Zend_Auth::getInstance()->getIdentity();
		/** Get our Action Helper */
		$this->_acl = $this->_helper->getHelper('acl');
	}

	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		$this->_redirect( $this->_contextPath."/inbox" );
	}

	public function inboxAction() {
		$account = $this->_auth->usr_mobile;
		$pmModel = new PmModel();

		//Begin: render script for paging.
		$paging = new Paging ( );
		//Get item on page.
		//Get total page.
		//Get current page.
		$itemPerPage = Constant::PRIVATE_MESSAGE_LIST_ITEM;
		$totalRecords = $pmModel->getTotalRecords ( $account );
		$currentPages = ( int ) $this->_request->getParam ( 'page' );
		if ($currentPages == 0)
		$currentPages = 1;
		//Set url for paging.
		//Render paging for page.
		//Render paging title for page.
		$url = $this->_baseUrl . "/" . $this->_contextPath . "/inbox";
		$page = $paging->doPaging ( $totalRecords, $url, $currentPages, $itemPerPage );
		$page_title = $paging->doPageSeparator ( $totalRecords, $currentPages, $itemPerPage );
		//Set for view.
		$this->view->assign ( "page", $page );
		$this->view->assign ( "page_title", $page_title );
		$this->view->assign ( "currentPage", $currentPages );
		//End: render script for paging.

		$messages = $pmModel->getAllMessage($account, $currentPages, $itemPerPage);
		$this->view->assign("messages", $messages);
		$this->render("inbox");
	}

	public function sendMessageAction(){
		if ($this->_request->isPost()){
			$request = $this->getRequest();
			$pm_from = $this->_auth->usr_mobile;
			$pm_to = $request->getPost("pm_to");
			$pm_title = $request->getPost("pm_title");
			$pm_content = $request->getPost("message_content");
			$pm_com = $request->getPost("pm_com");
			$pm = array(
				'pm_com' => $pm_com,
				'pm_from' => $pm_from,
				'pm_to' => $pm_to,
				'pm_title' => $pm_title,
				'pm_content' => $pm_content
			);
				
			$pmModel = new PmModel();
			$pmModel->insert($pm);							
		}
		$this->_redirect( $this->_contextPath."/inbox" );
	}

	public function sentAction() {

		$account = $this->_auth->usr_mobile;
		$pmModel = new PmModel();

		//Begin: render script for paging.
		$paging = new Paging ( );
		//Get item on page.
		//Get total page.
		//Get current page.
		$itemPerPage = Constant::PRIVATE_MESSAGE_LIST_ITEM;
		$totalRecords = $pmModel->getTotalRecordsSent( $account );
		$currentPages = ( int ) $this->_request->getParam ( 'page' );
		if ($currentPages == 0)
		$currentPages = 1;
		//Set url for paging.
		//Render paging for page.
		//Render paging title for page.
		$url = $this->_baseUrl . "/" . $this->_contextPath . "/sent";
		$page = $paging->doPaging ( $totalRecords, $url, $currentPages, $itemPerPage );
		$page_title = $paging->doPageSeparator ( $totalRecords, $currentPages, $itemPerPage );
		//Set for view.
		$this->view->assign ( "page", $page );
		$this->view->assign ( "page_title", $page_title );
		$this->view->assign ( "currentPage", $currentPages );
		//End: render script for paging.

		$messages = $pmModel->getAllMessageSent($account, $currentPages, $itemPerPage);
		$this->view->assign("messages", $messages);

		$this->render("sent");
	}

	public function viewAction() {
		$id = (int) $this->_request->getParam("id");
		$message = new PmModel();
		$mes = $message->getMessageById($id);
		/** Validate if the user is the owner */
		if (($mes->pm_to != $this->_auth->usr_mobile)) {
			$this->_acl->denyAccess();			
		}

		$commodity = new CommodityModel();
		$com = $commodity->getCommodityById($mes->pm_com);

		$this->view->assign("commodity", $com);
		$this->view->assign("message", $mes);
		$this->render("view");
	}

	public function replyAction() {
		$id = (int) $this->_request->getParam("id");
		$message = new PmModel();
		$mes = $message->getMessageById($id);
		$this->view->assign("message", $mes);
	}

	public function deleteAction() {		
		$id = (int) $this->_request->getParam("id");
		$message = new PmModel();
		$mes = $message->getMessageById($id);
		/** Validate if the user is the owner */
		if (($mes->pm_to != $this->_auth->usr_mobile)) {
			$this->_acl->denyAccess();			
		}
		
		$message->delete($id);
		$this->_redirect( $this->_contextPath."/inbox" );
	}

	public function deleteSentAction() {		
		$id = (int) $this->_request->getParam("id");
		$message = new PmModel();
		$mes = $message->getMessageById($id);
		/** Validate if the user is the owner */
		if (($mes->pm_from != $this->_auth->usr_mobile)) {
			$this->_acl->denyAccess();			
		}
		
		$message->delete($id);
		$this->_redirect( $this->_contextPath."/sent" );
	}
	
	public function finishedAction() {

	}
}
